package com.hc.demo_api820.controller;

import com.hc.demo_api820.domain.Users;
import com.hc.demo_api820.dto.ResultResponse;
import com.hc.demo_api820.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController   //因为我们是前后端分离项目   前端--json格式数据交互--后端
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 登录接口
     * 业务逻辑：Controller--》service--》Mapper
     * ①拿着手机号查询数据库看有没有这个用户，没有抛出异常，该手机号尚未注册
     * ②有该用户则进一步核对密码：密码错误，抛出异常，密码错误请重新登录
     * ③前面两关都过了，则用户合法，返回登录成功
     * */
    @PostMapping("/login")
    public ResultResponse login(@RequestBody Users users){
        System.out.println("users:"+users.getUserId()+","+users.getPassword());
        //return new ResultResponse(200,"登录成功");
        return userService.login(users);
    }
    @PostMapping("/register")
    public ResultResponse register(@RequestBody Users users){
        return userService.register(users);

    }

    /**
     * 校验手机号是否已经注册的接口
     * */
    @RequestMapping("/validatePhone")
    public ResultResponse validatePhone(@RequestBody Users users){
        return userService.validatePhone(users.getUserId());
    }


}
